有没有办法在不同的类型中重用相同的枚举值?我希望能够做到以下几点:enumDeviceState{UNKNOWN,ACTIVE,DISABLED,NOTPRESENT,UNPLUGGED};enumDeviceType{UNKNOWN,PLAYBACK,RECORDING};int_tmain(intargc,_TCHAR*argv[]){DeviceStatedeviceState=DeviceState::UNKNOWN;DeviceTypedeviceType=DeviceType::UNKNOWN;return0;}这对我来说很有意义,但对C++编译器没有意义——它提示:err
有没有办法在不同的类型中重用相同的枚举值?我希望能够做到以下几点:enumDeviceState{UNKNOWN,ACTIVE,DISABLED,NOTPRESENT,UNPLUGGED};enumDeviceType{UNKNOWN,PLAYBACK,RECORDING};int_tmain(intargc,_TCHAR*argv[]){DeviceStatedeviceState=DeviceState::UNKNOWN;DeviceTypedeviceType=DeviceType::UNKNOWN;return0;}这对我来说很有意义,但对C++编译器没有意义——它提示:err
有没有办法将文本文件中的字符串与枚举值相关联?问题是:我有一些枚举值作为字符串存储在一个文本文件中,我在满足某些条件时即时读取这些值...现在我想将读取的值分配给一个枚举。最有效的方法是什么?它不需要是最简单的方法。 最佳答案 您可以设置一个可以反复使用的map:templateclassEnumParser{mapenumMap;public:EnumParser(){};TParseSomeEnum(conststring&value){map::const_iteratoriValue=enumMap.find(value);
有没有办法将文本文件中的字符串与枚举值相关联?问题是:我有一些枚举值作为字符串存储在一个文本文件中,我在满足某些条件时即时读取这些值...现在我想将读取的值分配给一个枚举。最有效的方法是什么?它不需要是最简单的方法。 最佳答案 您可以设置一个可以反复使用的map:templateclassEnumParser{mapenumMap;public:EnumParser(){};TParseSomeEnum(conststring&value){map::const_iteratoriValue=enumMap.find(value);
我一直无法找到是否可以在枚举定义中使用关键字,例如:enumclassEServerAction{create,read,update,delete};在C#中,我可以使用@字符让编译器将其视为标识符。有没有办法在C++(VisualStudio2015)中做到这一点? 最佳答案 不,它们不能使用。来自MSDNKeywordsarepredefinedreservedidentifiersthathavespecialmeanings.Theycannotbeusedasidentifiersinyourprogram.rule对于
我一直无法找到是否可以在枚举定义中使用关键字,例如:enumclassEServerAction{create,read,update,delete};在C#中,我可以使用@字符让编译器将其视为标识符。有没有办法在C++(VisualStudio2015)中做到这一点? 最佳答案 不,它们不能使用。来自MSDNKeywordsarepredefinedreservedidentifiersthathavespecialmeanings.Theycannotbeusedasidentifiersinyourprogram.rule对于
std::byte是一种抽象,它应该提供对C++中内存区域的类型安全(r)访问,从新标准17开始。但是,它是根据这种方式声明的至http://en.cppreference.com/w/cpp/types/byte:enumclassbyte:unsignedchar{};也就是说,它是一个没有任何枚举的枚举类。因为通常enums的目的是提供一组受限的枚举,这似乎有点奇怪。具有私有(private)unsignedchar成员的类似乎是更明显的方法。为什么会这样? 最佳答案 标准不要求具有unsignedchar成员的class与u
std::byte是一种抽象,它应该提供对C++中内存区域的类型安全(r)访问,从新标准17开始。但是,它是根据这种方式声明的至http://en.cppreference.com/w/cpp/types/byte:enumclassbyte:unsignedchar{};也就是说,它是一个没有任何枚举的枚举类。因为通常enums的目的是提供一组受限的枚举,这似乎有点奇怪。具有私有(private)unsignedchar成员的类似乎是更明显的方法。为什么会这样? 最佳答案 标准不要求具有unsignedchar成员的class与u
为什么枚举值可以在C中定义的block之外访问,而在C++中却不能?考虑以下C程序。#includestructmystruct{enum{INT,FLOAT,STRING}type;intinteger;floatfloating_point;}tu;/*WhyisINTaccessiblehere?*/intmain(){tu.type=INT;tu.integer=100;return0;}它在C中编译并运行良好。但在C++中编译失败。#includestructmystruct{enum{INT,FLOAT,STRING}type;intinteger;floatfloatin
为什么枚举值可以在C中定义的block之外访问,而在C++中却不能?考虑以下C程序。#includestructmystruct{enum{INT,FLOAT,STRING}type;intinteger;floatfloating_point;}tu;/*WhyisINTaccessiblehere?*/intmain(){tu.type=INT;tu.integer=100;return0;}它在C中编译并运行良好。但在C++中编译失败。#includestructmystruct{enum{INT,FLOAT,STRING}type;intinteger;floatfloatin